package com.xc.service.Impl;

import com.xc.dao.SysUserMapper;
import com.xc.pojo.Role;
import com.xc.pojo.SysUser;
import com.xc.service.SysUserService;
import com.xc.utils.SysConstants;
import com.xc.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 通过部门id获取用户
     * @param deptId
     * @return
     */
    public int getUserByDeptId(Integer deptId) {
        return userMapper.getUserByDeptId(deptId);
    }
    /**
     * 根据角色编号查询该角色下的用户的数量
     * @param roleId
     * @return
     */
    public int getUserByRoleId(Integer roleId) {
        return userMapper.getUserByRoleId(roleId);
    }

    /**
     * 查询用户列表
     * @param userVo
     * @return
     */
    public List<SysUser> queryUserList(UserVo userVo) {
        return userMapper.queryUserList(userVo);
    }
    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    public int addUser(SysUser sysUser) {
        sysUser.setCreateDate(new Date());//创建时间
        //使用默认密码并进行加密处理
        sysUser.setPassword(new BCryptPasswordEncoder().encode(SysConstants.DEFAULT_PASSWORD));
        //默认为普通用户
        sysUser.setUserType(2);
        return userMapper.addUser(sysUser);
    }
    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    public int updateUser(SysUser sysUser) {
        sysUser.setModifyDate(new Date());//修改时间
        return userMapper.updateUser(sysUser);
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    public int deleteUser(Integer id) {
        //先删除用户关系角色表数据
        userMapper.deleteUserRoleByUserId(id);
        //删除用户数据
        return userMapper.deleteUser(id);
    }


    public int resetPwd(Integer id, Integer userId) {
        //创建用户对象
        SysUser sysUser = new SysUser();
        sysUser.setId(id);//用户ID
        sysUser.setModifyBy(userId);//修改人
        sysUser.setPassword(new BCryptPasswordEncoder().encode(SysConstants.DEFAULT_PASSWORD));
        sysUser.setModifyDate(new Date());
        return userMapper.updateUser(sysUser);
    }

    /**
     * 根据用户名查询用户信息
     * @param userName
     * @return
     */
    public SysUser getUserByUserName(String userName) {
        return userMapper.getUserByUserName(userName);
    }

    /**
     * 通过用户名查询用户
     * @param userName
     * @return
     */
    public SysUser queryUserByUserName(String userName) {
        return userMapper.queryUserByUserName(userName);
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //创建角色列表集合
        List<SimpleGrantedAuthority> authorities=new ArrayList<SimpleGrantedAuthority>();
        //调用根据用户姓名查询用户的方法
        SysUser sysUser = userMapper.queryUserByUserName(username);
        //循环遍历用户角色表
        for (Role role : sysUser.getRoleList()) {
            //将角色编码添加到角色集合中
            authorities.add(new SimpleGrantedAuthority(role.getRoleCode()));
        }
        //创建User对象 springSecurity里的User
        User user=new User(
                sysUser.getUserName(),
                sysUser.getPassword(),
                sysUser.getStatus()==1,
                true,
                true,
                true,
                authorities);

        System.out.println("sysUser"+sysUser);
        System.out.println("user"+user.getPassword());
        return user;
    }
}
